package com.rdc.qingkou.model;

import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.dto.CardStatusResultDTO;
import com.rdc.qingkou.dto.ResultDTO;
import com.rdc.qingkou.model.imodel.IBrowseDetailModel;
import com.rdc.qingkou.presenter.BrowseDetailPresenter;
import com.rdc.qingkou.retrofitService.IBrowseDetailService;
import com.rdc.qingkou.utils.MyUtils;

import java.net.HttpURLConnection;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;


public class BrowseDetailModel implements IBrowseDetailModel {

    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private BrowseDetailPresenter presenter;

    public BrowseDetailModel(BrowseDetailPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void clearDisposable() {
        mCompositeDisposable.dispose();
    }

    @Override
    public void addDisposable(Disposable disposable) {
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getCardStatus(int userId, int cardId) {
        MyUtils.getRetrofitProvider(URL.GET_CARD_STATUS).create(IBrowseDetailService.class)
                .getCardStatus(userId, cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<CardStatusResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<CardStatusResultDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getCardStatusError("获取卡片状态失败！error code:" + response.code());
                        } else if (response.body().getResult().equals("success")) {
                            presenter.getCardStatusSuccess(response.body().isCollected(), response.body().isAppreciate());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getCardStatusError("点赞/取消点赞失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void appreciate(int userId, int cardId) {
        MyUtils.getRetrofitProvider(URL.POST_PARISE_CARD).create(IBrowseDetailService.class)
                .appreciate(userId, cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> stringResponse) {
                        if (stringResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.appreciateError("点赞/取消点赞失败！error code:" + stringResponse.code());
                        } else if (stringResponse.body().getResult().equals("success")) {
                            presenter.appreciateSuccess(true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.appreciateError("点赞/取消点赞失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void cancelAppreciate(int userId, int cardId) {
        MyUtils.getRetrofitProvider(URL.POST_CANCEL_PARISE_CARD).create(IBrowseDetailService.class)
                .cancelAppreciate(userId, cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> stringResponse) {
                        if (stringResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.appreciateError("点赞/取消点赞失败！error code:" + stringResponse.code());
                        } else if (stringResponse.body().getResult().equals("success")) {
                            presenter.appreciateSuccess(false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.appreciateError("点赞/取消点赞失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void collect(int userId, int cardId) {
        MyUtils.getRetrofitProvider(URL.POST_COLLECTED_CARD).create(IBrowseDetailService.class)
                .collect(userId, cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> stringResponse) {
                        if (stringResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.collectError("收藏/取消收藏失败！error code:" + stringResponse.code());
                        } else if (stringResponse.body().getResult().equals("success")) {
                            presenter.collectSuccess(true);
                        } else {
                            presenter.collectError("收藏/取消收藏失败！");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.collectError("收藏/取消收藏失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void cancelCollect(int userId, int cardId) {
        MyUtils.getRetrofitProvider(URL.POST_CANCEL_COLLECTED_CARD).create(IBrowseDetailService.class)
                .cancelCollect(userId, cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> stringResponse) {
                        if (stringResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.collectError("收藏/取消收藏失败！error code:" + stringResponse.code());
                        } else if (stringResponse.body().getResult().equals("success")) {
                            presenter.collectSuccess(false);
                        } else {
                            presenter.collectError("收藏/取消收藏失败！");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.collectError("收藏/取消收藏失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void deleteCard(int cardId) {
        MyUtils.getRetrofitProvider(URL.POST_DELETE_CARD).create(IBrowseDetailService.class)
                .deleteCard(cardId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.deleteCardResult("删除卡片失败！error code:" + response.code(), false);
                        } else if (response.body().getResult().equals("success")) {
                            presenter.deleteCardResult("删除卡片成功！", true);
                        } else {
                            presenter.deleteCardResult("删除卡片失败！", false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.collectError("删除卡片失败！error message:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
